package com.mayn.util;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;



public class DriverFactory {
	
	/*
	public static String chromedriver = "D:\\tools\\chromedriver\\chromedriver.exe";
	public static String chromeBin = "C:\\Program Files (x86)\\Chrome\\chrome.exe";
	public static String chromeBin = "C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";
	public static String phantomjsDriverPath="D:\\tools\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe";
	*/
	public static String chromedriver = PropertiesUtil.getConfig("chromedriver");
	public static String chromeBin = PropertiesUtil.getConfig("chromeBin");
	public static String phantomjsDriver=PropertiesUtil.getConfig("phantomjsDriver");
	public static String firefoxDriver=PropertiesUtil.getConfig("firefoxDriver");
	
	public static String driver="chrome";
	
	
	public static WebDriver getWebDriver(ChromeOptions options){
		if(driver.equals("chrome")){
			return getChromeDriver(options);
		}		
		return null;
	}
	
	public static WebDriver getWebDriver(){
		if(driver.equals("chrome")){
			return getChromeDriver();
		}
		if(driver.equals("phantomjs")){
			return getPhantomjsDriver();
		}
		if(driver.equals("htmlunit")){
			return getHtmlunitDriver();
		}
		return null;
	}
	
	private static WebDriver getChromeDriver(ChromeOptions options) {
		System.setProperty("webdriver.chrome.driver", chromedriver);
		options.setBinary(chromeBin); 
		
		DesiredCapabilities capabilities = DesiredCapabilities.chrome();
		capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
		capabilities.setCapability(ChromeOptions.CAPABILITY,options);
		WebDriver driver = new ChromeDriver(options);
		return driver;
        
	}
		
	private static WebDriver getChromeDriver() {
		System.setProperty("webdriver.chrome.driver", chromedriver);
		ChromeOptions options = new ChromeOptions();
		options.addArguments("--test-type", "--start-maximized");
		options.setBinary(chromeBin); 
		DesiredCapabilities capabilities = DesiredCapabilities.chrome();
		capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
		capabilities.setCapability(ChromeOptions.CAPABILITY,options);
		WebDriver driver = new ChromeDriver(options);
		return driver;
        
	}
	
	/***
	 * HtmlUnitDriver不适应淘宝页面，放弃使用
	 * @return
	 */	
	public static WebDriver getHtmlunitDriver() {
		WebDriver driver = new EnhanceHtmlUnitDriver();
		return driver;
	}
	
	
	
	public static WebDriver getPhantomjsDriver() {
		System.getProperties().setProperty("phantomjs.binary.path", phantomjsDriver);
		WebDriver webDriver = new PhantomJSDriver();
		webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		return webDriver;
	}
	

	public static WebDriver getFirefoxDriver(FirefoxProfile fp) {
		System.getProperties().setProperty("webdriver.firefox.bin", firefoxDriver);
		WebDriver webDriver = new FirefoxDriver(fp);  

		return webDriver;
	}
	
	public static WebDriver getGeckoDriver() {
		System.setProperty("webdriver.gecko.driver", PropertiesUtil.getConfig("geckodriver"));  
        WebDriver webDriver = new FirefoxDriver();
		return webDriver;
	}
	
	
}
